<template>
  <page-header-wrapper>
    <a-card :bordered="false" class="card_style_re">
      <div class="searchBox">
        <div class="select">
          <span>会员名称：</span>
          <a-input placeholder="请输入会员名称" v-model="queryParam.name">
            <a-icon slot="suffix" type="search" />
          </a-input>
        </div>
        <a-button type="primary" @click="getList">查询</a-button>
      </div>
      <div class="table-operator" style="display: flex; justify-content: space-between; margin-top: 20px">
        <a-radio-group v-model="queryParam.status" @change="getList">
          <a-radio-button :value="-1"> 全部 </a-radio-button>
          <a-radio-button :value="0"> 未开始 </a-radio-button>
          <a-radio-button :value="1"> 进行中 </a-radio-button>
          <a-radio-button :value="2"> 已结束 </a-radio-button>
        </a-radio-group>
        <div>
          <a-dropdown>
            <a-menu slot="overlay">
              <a-menu-item @click="allDelete">批量删除</a-menu-item>
            </a-menu>
            <a-button> 批量操作 </a-button>
          </a-dropdown>
        </div>
      </div>
      <a-alert type="info" show-icon style="margin: 10px 0" v-if="selectedRowKeys.length">
        <span slot="message">
          已选择 <span style="color: red">{{ selectedRowKeys.length }}</span> 项
        </span>
      </a-alert>
      <a-table
        :rowKey="
          (r, i) => {
            return r.id
          }
        "
        :pagination="{
          total: this.totalCount,
          showSizeChanger: false,
          onChange: this.tablePageChange,
        }"
        :columns="columns"
        :rowSelection="{
          selectedRowKeys: this.selectedRowKeys,
          onChange: (e) => {
            this.selectedRowKeys = e
          },
        }"
        :data-source="list"
        :scroll="{ x: 'max-content' }"
      >
        <a-popconfirm
          placement="topLeft"
          slot="status"
          slot-scope="text, record"
          ok-text="确定"
          cancel-text="取消"
          @confirm="changeShow(record)"
        >
          <template slot="title">
            <p>{{ text == 1 ? '是否确定不显示' : '是否确定显示' }}</p>
          </template>
          <a-tag :color="text == 1 ? 'blue' : 'red'">{{ text | statusFilter }}</a-tag>
        </a-popconfirm>
        <!-- 最低消费金额 -->
        <span slot="min_price" slot-scope="text"> {{ text }}元 </span>

        <!-- 优惠券金额 -->
        <span slot="sub_price" slot-scope="text">{{ text }}元</span>

        <!-- 折扣率 -->
        <span slot="discount" slot-scope="text">{{ text }}折</span>

        <!-- 已领 -->
        <div slot="total_count" slot-scope="text, item">
          <span>{{ item.receive_count }}</span>
        </div>

        <!-- 剩余 -->
        <div slot="surplus_count" slot-scope="text">
          <span v-if="text > 0">{{ text }}</span>
          <span v-else>无限制</span>
        </div>

        <!-- 有效期 -->
        <div slot="expire_type" slot-scope="text, item">
          <span v-if="item.expire_type == 1 && item.expire_day != 0"> 领卷后当日起{{ item.expire_day }}天内可用 </span>
          <span v-else-if="item.expire_type == 1 && item.expire_day == 0">永久生效</span>
          <span v-if="item.expire_type == 2">{{ item.begin_time }}至{{ item.end_time }}</span>
        </div>

        <!-- 优惠卷数量 -->
        <div slot="coupon_status" slot-scope="text">
          <div v-if="text == 0" class="coupon-status-1">未开始</div>
          <div v-if="text == 1" class="coupon-status-1">进行中</div>
          <div v-if="text == 2" class="coupon-status-2">已结束</div>
        </div>

        <!-- 操作按钮 -->
        <span slot="action" slot-scope="text, record">
          <a-button type="link" @click="changeStatus(record, 1)" :disabled="record.business_status == 1" size="small"
            >发布</a-button
          >
          <a-divider type="vertical" style="margin: 0 1px" />
          <a-dropdown>
            <a-button type="link" size="small">更多<a-icon type="down" /> </a-button>
            <a-menu slot="overlay">
              <a-menu-item>
                <a-button
                  type="link"
                  @click="changeStatus(record, 0)"
                  :disabled="record.business_status == 0"
                  size="small"
                  >失效</a-button
                >
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>
      </a-table>
    </a-card>
  </page-header-wrapper>
</template>

<script>
import { getList, changeStatus } from '@/api/saas/marketing'
const columns = [
  {
    title: '优惠券ID',
    dataIndex: 'id',
  },
  {
    title: '商城名称',
    dataIndex: 'store_name',
  },
  {
    title: '优惠券名称',
    dataIndex: 'name',
  },
  {
    title: '优惠方式',
    dataIndex: 'discount_type_text',
    scopedSlots: {
      customRender: 'discount_type_text',
    },
  },
  {
    title: '已领',
    dataIndex: 'total_count',
    scopedSlots: {
      customRender: 'total_count',
    },
    // 总数减剩余为已领   剩余默认是-1  总数默认是 -1
    sorter: (a, b) => a.total_count - a.surplus_count - (b.total_count - b.surplus_count),
  },
  {
    title: '剩余',
    dataIndex: 'surplus_count',
    scopedSlots: {
      customRender: 'surplus_count',
    },
    sorter: (a, b) => a.surplus_count - b.surplus_count,
  },

  {
    title: '有效时间',
    dataIndex: 'expire_type',
    scopedSlots: {
      customRender: 'expire_type',
    },
  },
  {
    title: '状态',
    dataIndex: 'coupon_status',
    scopedSlots: {
      customRender: 'coupon_status',
    },
  },
  {
    title: '操作',
    width: '200',
    dataIndex: 'action',
    scopedSlots: {
      customRender: 'action',
    },
  },
]

export default {
  components: {},
  data() {
    return {
      // 搜索参数
      queryParam: {
        name: '',
        status: -1,
        is_business: 1,
        pageNo: 1,
        pageSize: 10,
        business_type: 1,
      },
      selectedRowKeys: [],
      columns,
      list: [],
      btnLoading: false,
    }
  },
  created() {
    this.getList()
  },
  methods: {
    // 分页请求
    tablePageChange(e) {
      this.queryParam.pageNo = e
      this.getList()
    },
    // 获取数据
    async getList() {
      const hide = this.$message.loading('加载中...', 1000)
      const res = await getList(this.queryParam)
      if (res.code == 0) {
        this.list = res.data.data
        this.totalCount = res.data.totalCount
      }
      hide()
    },
    // 切换商品上下架状态
    async changeStatus(record, status) {
      const that = this
      this.$confirm({
        content: '确定要' + status == 0 ? '取消发布' : '发布' + '该优惠券吗?',
        onOk() {
          changeStatus({ id: record.id, status }).then((res) => {
            if (res.code == 0) {
              that.getList()
            }
          })
        },
      })
    },
  },
}
</script>
<style lang="less" scoped>
.searchBox {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  /deep/ .ant-input-affix-wrapper {
    width: 220px;
    margin-right: 20px;
  }
  .select {
    margin-top: 11px;
    display: flex;
    align-items: center;
    margin-right: 15px;
  }
  .ant-btn {
    margin-top: 11px;
  }
}
.tip {
  line-height: normal;
  margin-bottom: 0;
  font-size: 14px;
  color: rgba(0, 0, 0, 0.45);
}
.name {
  display: flex;
  align-items: center;
  .pic {
    width: 48px;
    min-width: 48px;
    height: 48px;
    border-radius: 4px;
    overflow: hidden;
    margin-right: 24px;
    img {
      width: 100%;
      height: 100%;
    }
  }
  .info {
    flex: auto;
    width: 0;
    display: flex;
    flex-direction: column;
    font-size: 14px;
    span {
      width: 100%;
      overflow: hidden;
      white-space: nowrap;
      text-overflow: ellipsis;
    }
    span:nth-child(2) {
      color: rgba(0, 0, 0, 0.45);
      margin-top: 6px;
    }
  }
}
</style>
